//
//  BaseViewController.m
//  Bettle
//
//  Created by panhongliu on 2016/12/30.
//  Copyright © 2016年 panhongliu. All rights reserved.
//

#import "BaseViewController.h"
#import "MBProgressHUD+LJ.h"
#import "MJRefresh.h"
#import "UIScrollView+EmptyDataSet.h"
static const void *TbaleHeaderRereshBlockKey = &TbaleHeaderRereshBlockKey;

@interface BaseViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate,UITableViewDelegate,UITableViewDataSource>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentIndex=1;
    self.navigationController.navigationBar.translucent = NO;
        //自动调整scroview的插图
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.page=1;
    
    self.dataArray=[[NSMutableArray alloc]init];

    // Do any additional setup after loading the view.
}
-(void)setTitle:(NSString *)title
{
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:title fontSize:18];
}

//设置控制器的背景颜色
-(void)setViewBackgroundColor:(UIColor *)color
{
    if (color==nil) {
        color=UIColorFromRGB(0xf5f5f5);
    }
    self.view.backgroundColor=color;
    
}


-(void)addBackBtn
{
    __weak typeof(self) weakSelf = self;
    
    [self setLeftImageBarButtonItemWithFrame:CGRectMake(0, 0, 30, 30) image:@"btn_back" selectImage:nil action:^(WRBButton *button) {
        
        [weakSelf backBtn:button];
        
    }];
    
    
}
-(void)addLeftBackBtn:(NSString *)btnImg frame:(CGRect)frame
{
    __weak typeof(self) weakSelf = self;
    NSString *btnName;
    
    if (btnImg==nil) {
        btnName=@"btn_back";
    }
    else{
        btnName=btnImg;
    }
    
    [self setLeftImageBarButtonItemWithFrame:frame image:btnName selectImage:nil action:^(WRBButton *button) {
        
        [weakSelf backBtn:button];
        
    }];
    
    
}
-(void)addRightBtnWithframe:(CGRect)frame withImage:(NSString *)btnImg ishidden:(BOOL)hidden
{
    __weak typeof(self) weakSelf = self;
    NSString *btnName;
    
    if (btnImg==nil) {
        btnName=@"btn_back";
    }
    else{
        btnName=btnImg;
    }
    
    [self setRightImageBarButtonItemWithFrame:frame image:btnName selectImage:nil  ishidden:hidden  action:^(WRBButton *button) {
        
        [weakSelf BtnRight:button];
        
        
    }];
}

-(void)BtnRight:(WRBButton*)button
{
    NSLog(@"点击右侧按钮需要重写");
}


-(void)customTableView
{
    if(!_tableview){
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableview.showsVerticalScrollIndicator = NO;
        _tableview.scrollsToTop =YES;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        self.tableview.emptyDataSetSource = self;
        self.tableview.emptyDataSetDelegate = self;
        _tableview.backgroundColor=UIColorFromRGB(0xf5f5f5);
            [self setExtraCellLineHidden:_tableview];
        
        [self.view addSubview:_tableview];
    }
}
//隐藏多余线条
-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    
    UIView *view = [UIView new];
    
    //    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
}

-(void)registerNib:(NSString *)nibStr
{
    UINib *ordernib=[UIView loadNibNamed:nibStr bundle:nil];
    [self.tableview registerNib:ordernib forCellReuseIdentifier:nibStr];//注册cell
    
}

/**
 *  初始化一个自定义xib的tableView
 *
 *  @param nibString nib名字
 */
-(void)initWithTableViewNib:(NSString *)nibString didSelectCellBlock:(DidSelectCellBlock)block{
    
    [self customTableView];
    [self registerNib:nibString];
    if (self.didSelectCellBlock) {
        self.didSelectCellBlock=block;
 
    }
    
}



-(void)setTableHeaderRefresh:(TableViewHeaderFresh)fresh{
    
    self.currentIndex=1;

    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        /**
         *  用于重新请求数据用
         */
        fresh();
        
        
    }];
    
}
-(void)setCollectionViewHeaderRefresh:(TableViewHeaderFresh)fresh
{
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        /**
         *  用于重新请求数据用
         */
        fresh();
        
        
    }];

    self.currentIndex=1;

    
}


-(void)setFooterfresh:(TableViewFooterFresh )block{
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        /**
         *  添加上拉加载控件
         */
        
        block();
        
    }];
    
    
}


-(void)setCollectionViewFooterfresh:(TableViewFooterFresh )block
{
    
    self.collectionView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        /**
         *  添加上拉加载控件
         */
        if (block) {
            block();
            
        }
        
    }];
    
    
}

-(void)endReFresh{
    
    [self.tableview.mj_header endRefreshing];
    
    [self.tableview.mj_footer endRefreshing];
    
}

-(void)collectionViewEndReFresh
{
    [self.collectionView.mj_header endRefreshing];
    
    [self.collectionView.mj_footer endRefreshing];
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view=[UIView new];
    view.backgroundColor=UIColorFromRGB(0xf5f5f5);
    return view;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableCell"];
        //        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"基类里输出请重写方法");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.didSelectCellBlock) {
        self.didSelectCellBlock(indexPath) ;
        
    }
}

#pragma mark 空白页控制

//空白页图片

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *imageSring=nil;
    NSAssert(imageSring==nil, @"请设置默认图片");
    
    UIImage *imageTemp=IMAGE(imageSring);
    return imageTemp;
    
}
//是否允许滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

//空白页控制位置Y
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 0;
}


//空白页标题文本

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName: UIColorFromRGB(0x999999)};
    return [[NSAttributedString alloc] initWithString:@"重写此方法 不要的话置为空" attributes:attributes];
}

/**
 *  页面跳转
 *
 *  @param controller 跳转下一页
 */
-(void)pushNextViewController:(UIViewController *)controller;
{
    
    [self.navigationController pushViewController:controller animated:YES];
    
    
}

//返回
-(void)backBtn:(WRBButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - 添加加载等待效果
- (void)addProgressHUD
{
    [MBProgressHUD showMessage:@"正在加载"];
    
}

/**
 *  加载显示文字
 *
 *  @param statusLable 网络请求时显示文字不会自动隐藏需要手动删除
 */
-(void)showWithStatus:(NSString *)statusLable
{
    if (statusLable==nil) {
        [MBProgressHUD showMessage:@"正在加载"];
    }else
        [MBProgressHUD showMessage:statusLable];
    
}
//请求成功的提示文字
-(void)showSuccess:(NSString *)success
{
    if (success==nil) {
        
        [MBProgressHUD showSuccess:@"正在加载"];
    }
    else
        [MBProgressHUD showSuccess:success];
    
}

//请求失败的文字
-(void)showFailed:(NSString *)failed;
{
    if (failed==nil) {
        [MBProgressHUD showError:@"加载失败"];
        
    }
    [MBProgressHUD showError:failed];
    
}
//删除提示文字
-(void)removeStatuslable
{
    [MBProgressHUD hideHUD];
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
